package com.yuanye.gwcit;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.RequiresApi;

import com.elvishew.xlog.XLog;
import com.yuanye.gwcit.fragments.AudioFragment;
import com.yuanye.gwcit.fragments.AudioRecorderFragment;
import com.yuanye.gwcit.fragments.BluetoothFragment;
import com.yuanye.gwcit.fragments.CameraFragment;
import com.yuanye.gwcit.fragments.EmmcFragment;
import com.yuanye.gwcit.fragments.HdmiInFragment;
import com.yuanye.gwcit.fragments.HdmiOutFragment;
import com.yuanye.gwcit.fragments.HeadsetFragment;
import com.yuanye.gwcit.fragments.HeadsetRecorderFragment;
import com.yuanye.gwcit.fragments.KeyPressFragment;
import com.yuanye.gwcit.fragments.MacCheckFragment;
import com.yuanye.gwcit.fragments.RamCheckFragment;
import com.yuanye.gwcit.fragments.RtcSyncFragment;
import com.yuanye.gwcit.fragments.SnCheckFragment;
import com.yuanye.gwcit.fragments.SystemInfoFragment;
import com.yuanye.gwcit.fragments.UsbFragment;
import com.yuanye.gwcit.fragments.WifiFragment;
import com.yuanye.gwcit.utils.GT;
import com.yuanye.gwcit.utils.HttpHelper;
import com.yuanye.yeecho.base.BaseFragment;
import com.yuanye.yeecho.base.CommonActivity;
import com.yuanye.yeecho.bean.KeyDataBean;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class PcbaActivity extends CommonActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void addFragments() {
        enableAutoTest();
        addOneFragment(new SnCheckFragment());
        addOneFragment(new MacCheckFragment());
        addOneFragment(new SystemInfoFragment());
        addOneFragment(new RamCheckFragment());
        addOneFragment(new EmmcFragment());
        addOneFragment(new WifiFragment());
        addOneFragment(new BluetoothFragment());
        addOneFragment(new RtcSyncFragment());
//        addOneFragment(new NetPortFragment());
//        addOneFragment(new PcmLoopFragment());
        addOneFragment(new HeadsetFragment());
        addOneFragment(new HeadsetRecorderFragment());
        addOneFragment(new AudioFragment());
        addOneFragment(new AudioRecorderFragment());
//        addOneFragment(new CameraFragment());
        addOneFragment(new KeyPressFragment());
//        addOneFragment(new BrightFragment());
//        addOneFragment(new ScreenPanelFragment());
        addOneFragment(new UsbFragment());
//        addOneFragment(new TypecReverseFragment());
        addOneFragment(new HdmiInFragment());
        addOneFragment(new HdmiOutFragment());
    }

    @Override
    protected void uploadReport() {
        XLog.i("---------- KEY_DATA_START ---------");
        for (BaseFragment fragment: fragments){
            for (KeyDataBean bean: fragment.keyDataList){
                XLog.i(bean.toString());
            }
        }
        XLog.i("---------- KEY_DATA_END ---------");
        //todo
        if (true) return;
        String urlUploadReport = "http://" + GW.cfgPcba.getServerIp() +
                "/"+GW.cfgPcba.getApiRoute()+"/product/record/add";
        XLog.d("urlUploadReport:"+urlUploadReport);
        JSONObject main = new JSONObject();
        try {
            main.put("sn", GT.getBoardSn());
            main.put("mac", GT.getMacAddress());
            main.put("result", isAllPass ? "PASS": "FAIL");
            main.put("swVersion", GT.getAppVersion(this));
            main.put("swBranch", "PCBA["+GW.FIXTURE_ID+"]");
//            main.put("product", sp.getString(GW.PRODUCT, Result.A2145));
            main.put("product", GW.cfgPcba.getOrderCode());
            main.put("orderNo", "");
            main.put("details", GT.getLogString(GW.LOGFILE_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        progressDialog.setMessage("请求报告上传..");
        progressDialog.setCancelable(true);
        progressDialog.show();
        HttpHelper.post(urlUploadReport, String.valueOf(main), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                progressDialog.dismiss();
                showWarningDialog("网络错误，报告上传失败！\n" +
                        "url="+urlUploadReport+"\n" +
                        ""+e.getMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {
                    String json = response.body().string();
                    JSONObject jo = new JSONObject(json);
                    int code = jo.getInt("code");
                    String msg = jo.getString("msg");
                    if (code == 200){
                        toast("报告上传成功");
                        if (isAllPass){
                            progressDialog.setMessage("报告上传成功，准备过站");
                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    updateMesInfo();
                                }
                            }, 500);

                        }else{
                            progressDialog.dismiss();
                            showWarningDialog("报告已上传，测试未通过，不允许过站");
                        }

                    }else{
                        progressDialog.dismiss();
                        showWarningDialog("报告上传失败！\nmsg: "+msg+"\ncode: "+code);
                    }
                } catch (JSONException e) {
                    progressDialog.dismiss();
                    e.printStackTrace();
                    showWarningDialog("报告上传失败！\n"+e.getMessage());
                }
            }
        });
    }

    @Override
    protected void updateMesInfo() {
        progressDialog.setMessage("请求MES过站..");

        String urlUpdateMesInfo = "http://" + GW.cfgPcba.getServerIp() +
                "/"+GW.cfgPcba.getApiRoute()+"/product/custom/smtuploadmes";
        JSONObject json = new JSONObject();
        try {
            json.put("user", GW.OPERATOR_NAME);
            json.put("pswd", GW.OPERATOR_PSWD);
            json.put("sn", GT.getBoardSn());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpHelper.post(urlUpdateMesInfo, json.toString(), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                progressDialog.dismiss();
                showWarningDialog("网络错误，过站失败！\n" +
                        "url="+urlUpdateMesInfo +"\n"+
                        ""+e.getMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                progressDialog.dismiss();
                try {
                    String json = response.body().string();
                    JSONObject jo = new JSONObject(json);
                    int code = jo.getInt("code");
                    String msg = jo.getString("msg");
                    if (code == 200){
                        showWarningDialog("过站成功!");
                    }else{
                        showWarningDialog("过站失败!\nmsg: "+msg+"\ncode: "+code);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showWarningDialog("过站失败!\n" +e.getMessage());
                }
            }
        });
    }
}
